﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using PharmacyBussinesEntities;
using PharmacyService;

namespace PharmacyWeb.Almacen
{
    public partial class frmProveedor : System.Web.UI.Page
    {
        [Dependency]
        public IProveedorService ProveedorService { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            var accion = Request.QueryString["accion"];
            var idProveedor = Request.QueryString["id"];

            if (accion != null && accion == "nuevo")
            {
                fvProveedores.DefaultMode = FormViewMode.Insert;
            }
            else if (accion != null && accion == "eliminar" && idProveedor != null)
            {
                var id = Int32.Parse(idProveedor.ToString());

                ProveedorService.RemoveProveedor(id);
                Response.Redirect("FrmListProveedor.aspx");
            }

        }

        public Proveedor GetProveedor([QueryString("identificdor")] Int32? id)
        {
            Proveedor proveedor = null;
            if (id.HasValue)
                proveedor = ProveedorService.GetFromProveedorById(id.Value);
            return proveedor;
        }

        public void UpdateProveedor(Proveedor proveedor)
        {
            if (ModelState.IsValid)
            {
                ProveedorService.UpdateProveedor(proveedor);
                Response.Redirect("FrmListProveedor.aspx");
            }
        }

        public void InsertProveedor(Proveedor proveedor)
        {
            proveedor.Id = 0;
            if (ModelState.IsValid) 
            {
                ProveedorService.AddProveedor(proveedor);
                Response.Redirect("FrmListProveedor.aspx");
             }

    
                
            }
        }
    }
